Exclude One Column using dataframe.loc[]

We can exclude one column from the pandas dataframe by using the loc function. This function removes the column based on the location.

Syntax: dataframe.loc[ : , dataframe.columns!=’column_name’]

Here we will be using the loc() function with the given data frame to exclude columns with name,city, and cost in python.

Python3




# exclude name column
print(data.loc[:, data.columns != 'name'])
 
# exclude city column
print(data.loc[:, data.columns != 'city'])
 
# exclude cost column
print(data.loc[:, data.columns != 'cost'])


Output:

How to Exclude Columns

How to Exclude Columns in Pandas?

In this article, we will discuss how to exclude columns in pandas dataframe.

Similar Reads

Creating the DataFrame

Here we are creating the dataframe using pandas library in Python....

Exclude One Column using dataframe.loc[]

...

Exclude Multiple columns using dataframe.loc[]

We can exclude one column from the pandas dataframe by using the loc function. This function removes the column based on the location....

Removing the column from the dataframe

...